package com.ics.atable.chat.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * 自定义RedisTemplate
 *
 * @author haohongbin
 */
@Configuration
public class RedisConfig {

    @Value("${spring.data.redis.host}")
    private String host;

    @Value("${spring.data.redis.port}")
    private int port;

    @Value("${spring.data.redis.database}")
    private int database;

    @Bean
    @Primary
    public LettuceConnectionFactory defaultRedisConnectionFactory() {
        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
        config.setHostName(host);
        config.setPort(port);
        config.setDatabase(database); // 默认数据库
        return new LettuceConnectionFactory(config);
    }

    @Bean
    public LettuceConnectionFactory cacheRedisConnectionFactory() {
        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
        config.setHostName(host);
        config.setPort(port);
        config.setDatabase(1); // 缓存专用数据库
        return new LettuceConnectionFactory(config);
    }

    @Bean
    public LettuceConnectionFactory hotKeyRedisConnectionFactory() {
        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
        config.setHostName(host);
        config.setPort(port);
        config.setDatabase(2); // 同义词热点key专用数据库
        return new LettuceConnectionFactory(config);
    }


    @Bean
    @Primary
    public StringRedisTemplate defaultRedisTemplate(LettuceConnectionFactory defaultRedisConnectionFactory) {
        return createStringRedisTemplate(defaultRedisConnectionFactory);
    }

    @Bean
    public StringRedisTemplate cacheRedisTemplate(LettuceConnectionFactory cacheRedisConnectionFactory) {
        return createStringRedisTemplate(cacheRedisConnectionFactory);
    }

    @Bean
    public StringRedisTemplate hotKeyRedisTemplate(LettuceConnectionFactory hotKeyRedisConnectionFactory) {
        return createStringRedisTemplate(hotKeyRedisConnectionFactory);
    }

    /**
     * 配置通用的RedisTemplate
     *
     * @param connectionFactory Redis连接工厂
     * @return 配置好的RedisTemplate实例
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);

        // 设置序列化器
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new StringRedisSerializer());

        template.afterPropertiesSet();
        return template;
    }

    /**
     * 创建配置好的StringRedisTemplate实例
     *
     * @param connectionFactory Redis连接工厂
     * @return 配置好的StringRedisTemplate实例
     */
    private StringRedisTemplate createStringRedisTemplate(LettuceConnectionFactory connectionFactory) {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(connectionFactory);

        // 设置key和value的序列化器
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new StringRedisSerializer());

        // 启用事务支持（可选）
        template.setEnableTransactionSupport(true);

        return template;
    }
}
